package com.hake.core.jedis;

import org.apache.commons.lang3.StringUtils;

import com.hake.core.Constants;
import com.hake.core.exception.RedisException;
import com.hake.core.util.ProtoStuffSerializerUtil;

public class RedisKey {

	/**
	 * 将字符串Key 转换为字节数组 
	 * 默认已加上Redis 缓存前缀 hake:
	 * @param key
	 * @return byte[]
	 */
	public static final byte[] toBytes(String key) {
		if(StringUtils.isEmpty(key)) {
			throw new RedisException("Redis Key 值不能为空") ;
		}
		return ProtoStuffSerializerUtil.serialize(Constants.CACHE_NAMESPACE + key);
	}
	
	/**
	 * 将字节数组Key 转换为 字符串
	 * @param key
	 * @return String
	 */
	public static final String toString(byte[] key) {
		if(null != key && key.length > 0) {
			throw new RedisException("Redis Key 值不能为空") ;
		}
		return ProtoStuffSerializerUtil.deserialize(key, String.class) ;
	}
	
}
